package com.example.seh.chat;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.seh.R;
import com.example.seh.util.ChatUtil;
import com.example.seh.util.ImgUtil;
import com.example.seh.util.UserMsg;

import java.io.IOException;
import java.util.List;


public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder>{
    private Context context;
    private List<Msg> list;
    public MsgAdapter(Context context, List<Msg> list){
         this.context = context;
         this.list = list;
    }
    static class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout leftLayout, leftLayoutImg;
        TextView left_msg;
        ImageView left_img;
        ImageView left_avatar, left_avatar_img;

        LinearLayout rightLayout, rightLayoutImg;
        TextView right_msg;
        ImageView right_img;
        ImageView right_avatar, right_avatar_img;

        public ViewHolder(View view){
            super(view);
            leftLayout = view.findViewById(R.id.left_layout);
            leftLayoutImg = view.findViewById(R.id.left_layout_img);
            left_msg = view.findViewById(R.id.left_msg);
            left_img = view.findViewById(R.id.left_img);
            left_avatar = view.findViewById(R.id.you_avatar);
            left_avatar_img = view.findViewById(R.id.you_avatar_img);

            rightLayout = view.findViewById(R.id.right_layout);
            rightLayoutImg = view.findViewById(R.id.right_layout_img);
            right_msg = view.findViewById(R.id.right_msg);
            right_img = view.findViewById(R.id.right_img);
            right_avatar = view.findViewById(R.id.my_avatar);
            right_avatar_img = view.findViewById(R.id.my_avatar_img);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Msg msg = list.get(position);
//        if(msg.getReceivedOrSendType() == Msg.TYPE_RECEIVED){
        if(ChatUtil.msgList.get(position).getFromId() != UserMsg.userDTO.getUserId()){
            //如果是收到的消息，则显示左边的消息布局，将右边的消息布局隐藏
            holder.rightLayout.setVisibility(View.GONE);
            holder.rightLayoutImg.setVisibility(View.GONE);
            if (msg.getMsgOrImgType() == Msg.MSG) {
                //如果是收到的文本，则设置文本信息
                holder.left_msg.setText(msg.getContent());
                holder.leftLayout.setVisibility(View.VISIBLE);
                holder.leftLayoutImg.setVisibility(View.GONE);
                try {
                    if (!"".equals(ChatUtil.youAvatar)) {
//                        holder.left_avatar.setImageBitmap(ImgUtil.setUserAvatar(ChatUtil.youAvatar));
                        Glide.with(context).load(ChatUtil.youAvatar).into(holder.left_avatar);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {//如果是收到的图片，则设置图片信息
                holder.leftLayout.setVisibility(View.GONE);
                holder.leftLayoutImg.setVisibility(View.VISIBLE);
                try {
                    if (!"".equals(ChatUtil.youAvatar)) {
                        holder.left_avatar_img.setImageBitmap(ImgUtil.setUserAvatar(ChatUtil.youAvatar));
//                        Glide.with(context).load(ChatUtil.youAvatar).into(holder.left_avatar_img);
                    }
//                    holder.left_img.setImageBitmap(ImgUtil.stringToBitmap(msg.getBitmap()));
                    Glide.with(context).load(msg.getBitmap()).into(holder.left_img);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
//        }else if(msg.getReceivedOrSendType() == Msg.TYPE_SEND){
        }else if(ChatUtil.msgList.get(position).getFromId() == UserMsg.userDTO.getUserId()){
            //如果是发出的消息，则显示右边的消息布局，将左边的消息布局隐藏
            holder.leftLayout.setVisibility(View.GONE);
            holder.leftLayoutImg.setVisibility(View.GONE);
            if (msg.getMsgOrImgType() == Msg.MSG) {
                holder.right_msg.setText(msg.getContent());
                holder.rightLayout.setVisibility(View.VISIBLE);
                holder.rightLayoutImg.setVisibility(View.GONE);
                try {
                    if (!"".equals(ChatUtil.myAvatar)) {
//                        holder.right_avatar.setImageBitmap(ImgUtil.setUserAvatar(ChatUtil.myAvatar));
                        Glide.with(context).load(ChatUtil.myAvatar).into(holder.right_avatar);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else {
                //如果是发送的图片，则设置图片信息，显示右侧图片布局，隐藏左侧两布局，右侧文本布局
                holder.rightLayoutImg.setVisibility(View.VISIBLE);
                holder.rightLayout.setVisibility(View.GONE);
                try {
                    if (!"".equals(ChatUtil.myAvatar)) {
                        holder.right_avatar_img.setImageBitmap(ImgUtil.setUserAvatar(ChatUtil.myAvatar));
//                        Glide.with(context).load(ChatUtil.myAvatar).into(holder.right_avatar_img);
                    }
//                    holder.right_img.setImageBitmap(ImgUtil.stringToBitmap(msg.getBitmap()));
                    Glide.with(context).load(msg.getBitmap()).into(holder.right_img);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                holder.leftLayout.setVisibility(View.GONE);
                holder.leftLayoutImg.setVisibility(View.GONE);
                holder.rightLayout.setVisibility(View.GONE);
            }

        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
